package cn.javabb.project.controller;

import cn.javabb.common.model.domain.AjaxResult;
import cn.javabb.framework.web.controller.BaseController;
import cn.javabb.framework.web.page.TableDataInfo;
import cn.javabb.project.entity.SysTemplate;
import cn.javabb.project.entity.SysTemplateGroup;
import cn.javabb.project.service.TemplateGroupService;
import cn.javabb.project.service.TemplateService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2021/05/05 14:55
 */
@Api(tags = "模版组管理")
@RestController
@RequestMapping("/template/group")
public class TemplateGroupController extends BaseController {
    @Autowired
    private TemplateGroupService templateGroupService;

    @ApiOperation("查询所有模版组")
    @GetMapping("/")
    public AjaxResult list() {
        List<SysTemplateGroup> list = templateGroupService.list(new QueryWrapper<SysTemplateGroup>().orderByDesc("create_time"));
        return AjaxResult.success(list);
    }

    @ApiOperation("新增模版组")
    @PostMapping("/")
    public AjaxResult insert(@RequestBody SysTemplateGroup templateGroup) {
        templateGroup.setCreateTime(new Date());
        templateGroup.setUpdateTime(new Date());
        if (templateGroupService.save(templateGroup)) {
            return AjaxResult.success("保存成功");
        }
        return AjaxResult.success("保存失败");
    }

    @ApiOperation("更新模版组")
    @PutMapping("/")
    public AjaxResult update(@RequestBody SysTemplateGroup templateGroup) {
        templateGroup.setUpdateTime(new Date());
        if (templateGroupService.updateById(templateGroup)) {
            return AjaxResult.success("保存成功");
        }
        return AjaxResult.success("保存失败");
    }

    @ApiOperation("删除模版组")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        if (templateGroupService.deleteById(id)) {
            return AjaxResult.success("刪除成功");
        }
        return AjaxResult.success("删除失败");
    }

}
